/**
 * @Author ZhaoLei
 * @FileName HftApi.js
 * @Description  hft api
 * @Date 2022-10-28 17:14
 * @Copyright By hftsoft
 */
import api from '@/utils/http/impl/axios_api_instance';
import { Toast } from "vant";

const apiLinkHost = process.env.API_HFT_LINK_HOST;
const apiAjiaHost = process.env.API_HFT_AJIA_HOST;

const postRequest = (url, data, config = {}) => api.postNew(url, data, config).then(response => response.data || {});

const baseHftApiPost = (url, data = {}) => {
  let host = apiLinkHost;

  if (window.hasOwnProperty('zshft') && url === '/mobileWeb/userInfo/sendOpenAnjiaMemberToBrokerManager') {
    host = window.zshft.OnJsRootPojectType() == 2 ? apiAjiaHost : apiLinkHost;
  }

  let config = {
    headers: {
      'U-FS': 'ZHIYE'
    }
  };
  if (window.hasOwnProperty('zshft')) {
    config.headers.CLIENTKEY = window.zshft.OnJsGetClientKey();
  }

  return new postRequest(`${ host }${ url }`, data, config).then(res => {
    const { errCode, data = {}, errMsg } = res;
    if (errCode === 200) return Promise.resolve(data);
    Toast(errMsg || '请求失败');
  });
};

const getCourseListByHft = () => baseHftApiPost('/liveWeb/openApi/zhiye/enterpriseUniversity/getCourse');

const getMemberInfo = (archiveId) => baseHftApiPost('/liveWeb/openApi/AnjiaMember/checkOpen', { archiveId });
const getZhiYeSign = (archiveId) => baseHftApiPost('/liveWeb/openApi/AnjiaMember/getZhiyeSign', { archiveId });

const sendOpenAnjiaMemberToBrokerManager = () => baseHftApiPost('/mobileWeb/userInfo/sendOpenAnjiaMemberToBrokerManager', {});

export default {
  getCourseListByHft,
  getMemberInfo,
  getZhiYeSign,
  sendOpenAnjiaMemberToBrokerManager
};
